function getCookie(c_name)//获取cookie内容
{
    if (document.cookie.length>0)
    {
        c_start=document.cookie.indexOf(c_name + "=")
        if (c_start!=-1)
        {
            c_start=c_start + c_name.length+1
            c_end=document.cookie.indexOf(";",c_start)
            if (c_end==-1) c_end=document.cookie.length
            return unescape(document.cookie.substring(c_start,c_end))
        }
    }
    return ""
}
var username=getCookie('username');
if(username=='')
{
    setTimeout('location.href="login.html";',0);
}

var biao=0;
window.onload=function(){
    var wzy='';
    wzy += "<th>" + "勾选"+ "</th>"+"<th>" + "题号"+ "</th>"+"<th>" + "题目内容"+ "</th>"+"<th>" + "答案"+ "</th>";
    document.getElementById('thd').innerHTML = "";
    $("#thd").append(wzy);
    $.ajax({
        type:"GET",
        dataType: "json",
        contentType: "application/json",
        url:"/tfng/findAll",
        success:function(data){
            var wzy1='';
            document.getElementById('tedit').innerHTML = "";
            for(var i=0;i<data.length;i++) {
                var j = i + 1;
                wzy1 += "<tr>" + "<td>" + "<input  type=\"radio\" name=\"personality\" id='" + data[i].id + "' >" + "</td>" + "<th>" + j + "</th>" + "<th contentEditable='true'>" + data[i].content + "</th>" + "<th contentEditable='true'>" + data[i].answer + "</th>" + "</tr>";
            }
            $("#tedit").append(wzy1);
        },
        error: function () {
            alert("查询失败");
        }
    })
}
function tf(){
    biao=0
    var wzy='';
    wzy += "<th>" + "勾选"+ "</th>"+"<th>" + "题号"+ "</th>"+"<th>" + "题目内容"+ "</th>"+"<th>" + "答案"+ "</th>";
    document.getElementById('thd').innerHTML = "";
    $("#thd").append(wzy);
    $.ajax({
        type:"GET",
        dataType: "json",
        contentType: "application/json",
        url:"/tfng/findAll",
        success:function(data){
            var wzy1='';
            document.getElementById('tedit').innerHTML = "";
            for(var i=0;i<data.length;i++) {
                var j = i + 1;
                wzy1 += "<tr>" + "<td>" + "<input  type=\"radio\" name=\"personality\" id='" + data[i].id + "' >" + "</td>" + "<th>" + j + "</th>" + "<th contentEditable='true'>" + data[i].content + "</th>" + "<th contentEditable='true'>" + data[i].answer + "</th>" + "</tr>";
            }
            $("#tedit").append(wzy1);
        },
        error: function () {
            alert("查询失败");
        }
    })
}
function sg(){
    biao=1;
    var wzy='';
    wzy += "<th>" + "勾选"+ "</th>"+"<th>" + "题号"+ "</th>"+"<th>" + "题目内容"+ "</th>"+"<th>" + "A"+ "</th>"+"<th>" + "B"+ "</th>"+"<th>" + "C"+ "</th>"+"<th>" + "D"+ "</th>"+"<th>" + "答案"+ "</th>";
    document.getElementById('thd').innerHTML = "";
    $("#thd").append(wzy);
    $.ajax({
        type:"GET",
        dataType: "json",
        contentType: "application/json",
        url:"/single/findAll",
        success:function(data){
            var wzy1='';
            document.getElementById('tedit').innerHTML = "";
            for(var i=0;i<data.length;i++) {
                var j = i + 1;
                wzy1 += "<tr>" + "<td>" + "<input  type=\"radio\" name=\"personality\" id='" + data[i].id + "' >" + "</td>" + "<th>" + j + "</th>" + "<th contentEditable='true'>" + data[i].content + "</th>" + "<th contentEditable='true'>" + data[i].option1 + "</th>" + "<th contentEditable='true'>" + data[i].option2 + "</th>"+ "<th contentEditable='true'>" + data[i].option3 + "</th>"+ "<th contentEditable='true'>" + data[i].option4 + "</th>"+ "<th contentEditable='true'>" + data[i].answer+ "</th>"+"</tr>";
            }
            $("#tedit").append(wzy1);
        },
        error: function () {
            alert("查询失败");
        }
    })

}
function multi(){
    biao=2;
    var wzy='';
    wzy += "<th>" + "勾选"+ "</th>"+"<th>" + "题号"+ "</th>"+"<th>" + "题目内容"+ "</th>"+"<th>" + "A"+ "</th>"+"<th>" + "B"+ "</th>"+"<th>" + "C"+ "</th>"+"<th>" + "D"+ "</th>"+"<th>" + "答案"+ "</th>";
    document.getElementById('thd').innerHTML = "";
    $("#thd").append(wzy);
    $.ajax({
        type:"GET",
        dataType: "json",
        contentType: "application/json",
        url:"/multi/findAll",
        success:function(data){
            var wzy1='';
            document.getElementById('tedit').innerHTML = "";
            for(var i=0;i<data.length;i++) {
                var j = i + 1;
                var manswer='';
                for(var z=0;z<data[i].answers.length;z++)
                {
                    manswer+=data[i].answers[z].content;
                }
                wzy1 += "<tr>" + "<td>" + "<input  type=\"radio\" name=\"personality\" id='" + data[i].id + "' >" + "</td>" + "<th>" + j + "</th>" + "<th contentEditable='true'>" + data[i].content + "</th>" + "<th contentEditable='true'>" + data[i].option1 + "</th>" + "<th contentEditable='true'>" + data[i].option2 + "</th>"+ "<th contentEditable='true'>" + data[i].option3 + "</th>"+ "<th contentEditable='true'>" + data[i].option4 + "</th>"+ "<th contentEditable='true'>"+manswer+"</th>"+"</tr>";
            }
            $("#tedit").append(wzy1);
        },
        error: function () {
            alert("查询失败");
        }
    })
}

function edit1(){
    if(biao==0)
    {
        var nodes = document.getElementsByTagName("input");
        var biao1=0;
        var s;
        for(var i=0;i<nodes.length;i++)
        {
            if(nodes[i].type=="radio" && nodes[i].checked) {
                s = parseInt(nodes[i].id);
                biao1=1;
            }
        }
        if(biao1==0)
        {
            alert('请勾选')
        }
        else {
            var mytable = document.getElementById('tableedit');
            for (var i = 1, rows = mytable.rows.length; i < rows; i++) {
                if (mytable.rows[i].cells[0].getElementsByTagName("INPUT")[0].id == s) {
                    var content= mytable.rows[i].cells[2].innerHTML;
                    var answer =mytable.rows[i].cells[3].innerHTML;
                }
            }
            var tf = [{
                "answer": answer,
                "content": content,
                "id": s
            }];
            $.ajax({
                type: "POST",
                dataType: "json",
                contentType: "application/json",
                url: "/tfng/update",
                data: JSON.stringify(tf),
                success: function (data) {
                    alert("保存成功");
                    location.reload();
                },
                error: function () {
                    alert("保存失败");
                }
            })
        }
    }
    else if(biao==1)
    {
        var nodes = document.getElementsByTagName("input");
        var biao1=0;
        var s;
        for(var i=0;i<nodes.length;i++)
        {
            if(nodes[i].type=="radio" && nodes[i].checked) {
                s = parseInt(nodes[i].id);
                biao1=1;
            }
        }
        if(biao1==0)
        {
            alert('请勾选')
        }
        else {
            var mytable = document.getElementById('tableedit');
            for (var i = 1, rows = mytable.rows.length; i < rows; i++) {
                if (mytable.rows[i].cells[0].getElementsByTagName("INPUT")[0].id == s) {
                    var content= mytable.rows[i].cells[2].innerHTML;
                    var option1 =mytable.rows[i].cells[3].innerHTML;
                    var option2 =mytable.rows[i].cells[4].innerHTML;
                    var option3=mytable.rows[i].cells[5].innerHTML;
                    var option4 =mytable.rows[i].cells[6].innerHTML;
                    var answer =mytable.rows[i].cells[7].innerHTML;
                }
            }
            var singleChoices =[ {
                "answer": answer,
                "content": content,
                "id": s,
                "option1": option1,
                "option2": option2,
                "option3": option3,
                "option4": option4
            }];
            $.ajax({
                type: "POST",
                dataType: "json",
                contentType: "application/json",
                url: "/single/update",
                data: JSON.stringify(singleChoices),
                success: function (data) {
                    alert("保存成功");
                    location.reload();
                },
                error: function () {
                    alert("保存失败");
                }
            })
        }
    }
    else{
        var nodes = document.getElementsByTagName("input");
        var biao1=0;
        var s;
        for(var i=0;i<nodes.length;i++)
        {
            if(nodes[i].type=="radio" && nodes[i].checked) {
                s = parseInt(nodes[i].id);
                biao1=1;
            }
        }
        if(biao1==0)
        {
            alert('请勾选')
        }
        else {
            var mytable = document.getElementById('tableedit');
            for (var i = 1, rows = mytable.rows.length; i < rows; i++) {
                if (mytable.rows[i].cells[0].getElementsByTagName("INPUT")[0].id == s) {
                    var content= mytable.rows[i].cells[2].innerHTML;
                    var option1 =mytable.rows[i].cells[3].innerHTML;
                    var option2 =mytable.rows[i].cells[4].innerHTML;
                    var option3=mytable.rows[i].cells[5].innerHTML;
                    var option4 =mytable.rows[i].cells[6].innerHTML;
                    var answer =mytable.rows[i].cells[7].innerHTML;
                }
            }
            var answer1=[];
            for(var z=0;z<answer.length;z++)
            {
                var answerid=s;
                switch (answer[z]){
                    case 'A':answerid+=1;break;
                    case 'B':answerid+=2;break;
                    case 'C':answerid+=3;break;
                    case 'D':answerid+=4;break;
                }
                answer1[z]= {
                    "content": answer[z],
                    "id": answerid
                }
            }

            var multiChoices =[{
                "answers": answer1,
                "content": content,
                "id": s,
                "option1": option1,
                "option2": option2,
                "option3": option3,
                "option4": option4
            }];
            $.ajax({
                type: "POST",
                dataType: "json",
                contentType: "application/json",
                url: "/multi/update",
                data: JSON.stringify(multiChoices),
                success: function (data) {
                    alert("保存成功");
                    location.reload();
                },
                error: function () {
                    alert("保存失败");
                }
            })
        }
    }
}

function del(){
    if(biao==0)
    {
        var nodes = document.getElementsByTagName("input");
        var biao1=0;
        var s;
        for(var i=0;i<nodes.length;i++)
        {
            if(nodes[i].type=="radio" && nodes[i].checked) {
                s = parseInt(nodes[i].id);
                biao1=1;
            }
        }
        if(biao1==0)
        {
            alert('请勾选')
        }
        else {
            var ids=[];
            ids[0]=s;
            $.ajax({
                type: "POST",
                dataType: "json",
                contentType: "application/json",
                url: "/tfng/delete",
                data: JSON.stringify(ids),
                success: function (data) {
                    alert("删除成功");
                    location.reload();
                },
                error: function () {
                    alert("删除失败");
                }
            })
        }
    }
    else if(biao==1)
    {
        var nodes = document.getElementsByTagName("input");
        var biao1=0;
        var s;
        for(var i=0;i<nodes.length;i++)
        {
            if(nodes[i].type=="radio" && nodes[i].checked) {
                s = parseInt(nodes[i].id);
                biao1=1;
            }
        }
        if(biao1==0)
        {
            alert('请勾选')
        }
        else {
            var ids=[];
            ids[0]=s;
            $.ajax({
                type: "POST",
                dataType: "json",
                contentType: "application/json",
                url: "/single/delete",
                data: JSON.stringify(ids),
                success: function (data) {
                    alert("删除成功");
                    location.reload();
                },
                error: function () {
                    alert("删除失败");
                }
            })
        }
    }
    else{
        var nodes = document.getElementsByTagName("input");
        var biao1=0;
        var s;
        for(var i=0;i<nodes.length;i++)
        {
            if(nodes[i].type=="radio" && nodes[i].checked) {
                s = parseInt(nodes[i].id);
                biao1=1;
            }
        }
        if(biao1==0)
        {
            alert('请勾选')
        }
        else {
            var ids=[];
            ids[0]=s;
            $.ajax({
                type: "POST",
                dataType: "json",
                contentType: "application/json",
                url: "/multi/delete",
                data: JSON.stringify(ids),
                success: function (data) {
                    alert("删除成功");
                    location.reload();
                },
                error: function () {
                    alert("删除失败");
                }
            })
        }
    }
}